<template>
    <div class="table-page-search-wrapper">
        <div class="table-operator">
            <a-button shape="round" v-if="isShowButton" @click="openCommit" type="primary" icon="arrow-up" >提交</a-button>
            <a-button icon="rest" shape="round" v-if="isCancel" @click="cancel" type="danger" >撤销</a-button>
        </div>
        <a-spin :spinning="confirmLoading">
            <LForm ref="contractTaskForm" :formAttributes="formAttributes" :mostColumns="mostColumns" :labelCol="labelCol" :wrapperCol="wrapperCol"></LForm>
        </a-spin>
        <div style="height:30px;"></div>

        <a-tabs default-active-key="1" style="padding-left:10px;">
            <a-tab-pane key="1" tab="合同详情">
                <div class="table-operator" v-if="isShowBusTable">
                    <a-button v-if="isCancel" shape="round" size="small" type="primary" icon="plus" @click="sysPageOpen('1744259764370747394',{contractCode:formData.contractCode},'ExtModal','添加车辆信息');">添加车辆</a-button>
                </div>
                <LTable  v-if="isShowBusTable"
                    ref="list"
                    size="middle"
                    rowKey="keyId"
                    :columns="columns"
                    axiosUrl="/WxContractDetailController/page"
                    :showPagination="true"
                    :showQueryForm="true"
                    :isAutoLoad="false"
                >
                    <span slot="action" slot-scope="text, record" v-if="isCancel">
                        <a @click="edit(record)">详情</a>
                        <a-divider type="vertical" />
                        <a @click="tableDel({keyId:record.keyId},'/WxContractDetailController/del','list')">删除</a>
                        
                    </span>
                </LTable>
            </a-tab-pane>
            <a-tab-pane key="2" tab="审批详情">

                <a-timeline v-if="approvalOpinions.length>0">
                    <a-timeline-item v-for="(opinion,idx) in approvalOpinions" :key="idx">
                        
                        <a-icon v-if="opinion.isPass=='1'" slot="dot" type="check-circle" style="font-size: 16px;" />
                        <a-icon v-else slot="dot" type="close-circle" style="font-size: 16px;" />
                       
                        <a-row>
                            <a-col :span="2">
                                <template v-if="opinion.userAvatar">
                                    <a-avatar size="large" :src="defaultSettings.fileServer+'/'+opinion.userAvatar"></a-avatar>
                                </template>
                                <template v-else>
                                    <a-avatar size="large" :src="require('/src/assets/default_photo.png')"></a-avatar>
                                </template>
                                <div>{{opinion.userName}}</div>
                                
                            </a-col>
                            <a-col :span="22">
                                {{opinion.time}}
                                <div>{{opinion.message}}</div> 
                                <div>
                                    <template v-if="opinion.userSign">
                                        <img style="width:80px;" :src="defaultSettings.fileServer+'/'+opinion.userSign" />
                                    </template>
                                </div>
                            </a-col>
                        
                        </a-row>
                       
                    </a-timeline-item>
                    
                </a-timeline>
                
                
            </a-tab-pane>
        </a-tabs>
       
        <commit-modal ref="contractCommit" @handleOk="commitFlow" @handlerChange="handlerChange"></commit-modal>
        <ExtDrawer ref="ExtDrawer" @close="refresh('list')"></ExtDrawer>
        <ExtModal ref="ExtModal" @close="refresh('list')" ></ExtModal>
    </div>

    
</template>

<script>
import LForm from '@/components/lt/form/LForm'
import pick from 'lodash.pick'
import lodash from 'lodash'
import request from '@/utils/request'
import notification from 'ant-design-vue/es/notification'
import commitModal from '@/views/flowable/view/commitModal'
import { Modal } from 'ant-design-vue'
import defaultSettings from '@/config/defaultSettings'
import ExtDrawer from '@/views/system/codeGenerator/ExtDrawer'
import ExtModal from '@/views/system/codeGenerator/ExtModal'
import LTable from '@/components/lt/LTable'
import {SysMixin} from '@/mixins/SysMixin'
export default {
    name: 'contractVehicleTaskForm',
    components: { LForm, commitModal,LTable,ExtDrawer,ExtModal},
    mixins:[SysMixin],
    props: {
        componentParam:{
            type:Object,
            default:()=>{}
        }
    },
    data() {
        return {
            defaultSettings,
            confirmLoading: false,
            isShowButton:true,
            isCancel:false,
            visible:false,
            description:null,//环节说明，用来控制现实内容
            formData: {},
            taskInfo: {},
            mostColumns: 1,
            labelCol: {sm: { span: 6 }},
            wrapperCol: {sm: { span: 18 }},
            formAttributes: {
                type:'',
                formItems: [
                    {
                        title:'合同审批',
                        columns:2,
                        fields: [
                            {field:'contractCode',label:'合同编号',component:'input',disabled:true,rules: [{required: false, message: '不能为空!'}]},
                            {field:'contractName',label:'合同名称',component:'input',disabled:true,rules: [{required: false, message: '不能为空'}]},
                            {field:'partyA',label:'甲方',component:'select',disabled:true,dictCode:defaultSettings.数据字典.部队信息,rules: [{required: false, message: '不能为空!'}]},
                            {field:'signedDate',label:'签到日期',component:'date',disabled:true,rules: [{required: false, message: '不能为空'}]},
                            {field:'isLocal',label:'是否现地',component:'select',disabled:true,dictCode:defaultSettings.数据字典.是否,rules: [{required: false, message: '不能为空!'}]},
                            {field:'budgetAmount',label:'预算',component:'inputNumber',disabled:true,rules: [{required: false, message: '不能为空'}]},
                            {field:'equipNum',label:'预计台数',component:'inputNumber',disabled:true,rules: [{required: false, message: '不能为空!'}]},   
                        ]
                    },
                     {
                        title:'合同审批',
                        columns:2,
                        fields: [
                            {field:'contractAnnex',label:'合同附件',component:'fileUp',disabled:true},
                            
                        ]
                    }
                ]
            },
            approvalOpinions:[],
            columns:[
                {
                    title: '序号',
                    dataIndex: '',
                    key:'rowIndex',
                    width:60,
                    align:"center",
                    customRender:function (t,r,index) {
                      return parseInt(index)+1;
                    }
                },
                 {
                    title: '送修单位',
                    dataIndex: 'sendRepairOrg',
                    customRender:(text)=>{
                        if(!text){
                            return ''
                        }else{
                            return this.dictText(defaultSettings.数据字典.部队信息,text)
                        }
                    }
                },
                
                {
                    title: '型号',
                    dataIndex: 'vehicleModel',
                    customRender:(text)=>{
                        if(!text){
                            return ''
                        }else{
                            return this.dictText(defaultSettings.数据字典.车型,text)
                        }
                    }
                },
                {
                    title: '牌照号',
                    dataIndex: 'plateNo'
                    
                },
                {
                    title: '底盘号',
                    dataIndex: 'chassisNo'
                    
                },
                {
                    title: '发动机号',
                    dataIndex: 'engineNo'
                },
                {
                    title: '车身颜色',
                    dataIndex: 'vehicleColor',
                    customRender:(text)=>{
                        if(!text){
                            return ''
                        }else{
                            return this.dictText(defaultSettings.数据字典.车身颜色,text)
                        }
                    }
                },
                {
                    title: '出厂日期',
                    dataIndex: 'factoryDate'
                },
                {
                    title: '列装日期',
                    dataIndex: 'useDate'
                },
                {
                    title: '运营里程',
                    dataIndex: 'runKm'
                },
                {
                    title: '预算',
                    dataIndex: 'budgetAmount'
                },
                // {
                //     title: '操作',
                //     dataIndex: 'action',
                //     width: 120,
                //     scopedSlots: { customRender: 'action' }
                // }
            ],
            initDictCodes:defaultSettings.数据字典.部队信息+','+defaultSettings.数据字典.车身颜色+','+defaultSettings.数据字典.车型+','+defaultSettings.数据字典.车辆装备计量单位,
            isShowBusTable:true,
        }
    },
    watch: {
        'componentParam': {
            deep: true,
            immediate: true,
            handler(newV, oldV) {
                if (newV) {
                    this.taskInfo = newV
                    this.formData = {}
                    this.$nextTick(() => {
                        this.$refs['contractTaskForm'].getForm().resetFields()
                        this.getFormData()
                    })
                } else {
                    this.taskInfo = {}
                    this.formData = {}
                    this.$nextTick(() => {
                        this.$refs['contractTaskForm'].getForm().resetFields()
                    })
                }
                
            }
        }
    },
    methods: {
        handlerChange(changeItem){

        },
        commitFlow(data) {
            let param = {}
            param = Object.assign({}, param, this.formData)
            param.templateId = this.taskInfo.templateId
            param.taskId = this.taskInfo.id
            if (data) {
                param = Object.assign({}, param, data)
            }
            this.confirmLoading = true
            request({url: '/WxContractController/commit', method: 'post', data:param}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                    this.isShowButton = false
                    this.isCancel = false
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
    
        openCommit() {

            if(!this.taskInfo){
                notification.error({ message: '系统提示', description: "当前任务环节信息不存在"})
                return false
            }
            this.$refs['contractCommit'].open(this.taskInfo,this.formData)
       
        },
        getFormData() {
            this.confirmLoading = true
            request.get('/WxContractController/queryByInstanceId',{params:{instanceId: this.taskInfo.procInstId}}).then(res => {
                if(res.success){
                    if(res.result.contractType==this.defaultSettings.常量.工程装备维修合同类型 || res.result.contractType==this.defaultSettings.常量.整车和总成维修合同类型){
                        this.isShowBusTable = true
                    }else{
                        this.isShowBusTable = false
                    }
                    if(res.result.contractType==this.defaultSettings.常量.工程装备维修合同类型){
                        this.columns=[
                            {
                                title: '序号',
                                dataIndex: '',
                                key:'rowIndex',
                                width:60,
                                align:"center",
                                customRender:function (t,r,index) {
                                return parseInt(index)+1;
                                }
                            },
                            {
                                title: '送修单位',
                                dataIndex: 'sendRepairOrg',
                                customRender:(text)=>{
                                    if(!text){
                                        return ''
                                    }else{
                                        return this.dictText(defaultSettings.数据字典.部队信息,text)
                                    }
                                }
                            },
                            
                            {
                                title: '型号',
                                dataIndex: 'vehicleModel',
                                customRender:(text)=>{
                                    if(!text){
                                        return ''
                                    }else{
                                        return this.dictText(defaultSettings.数据字典.车型,text)
                                    }
                                }
                            },
                             {
                                title: '单价',
                                dataIndex: 'unitPrice'
                            },
                            {
                                title: '预算',
                                dataIndex: 'budgetAmount'
                            },
                            {
                                title: '设备号',
                                dataIndex: 'plateNo'
                                
                            },
                            {
                                title: '联系人',
                                dataIndex: 'contactPerson'
                                
                            },
                            {
                                title: '联系电话',
                                dataIndex: 'contactPhone'
                            },
                            {
                                title: '计量单位',
                                dataIndex: 'measureUnit',
                                customRender:(text)=>{
                                    if(!text){
                                        return ''
                                    }else{
                                        return this.dictText(defaultSettings.数据字典.车辆装备计量单位,text)
                                    }
                                }
                            },
                           
                           
                            // {
                            //     title: '操作',
                            //     dataIndex: 'action',
                            //     width: 120,
                            //     scopedSlots: { customRender: 'action' }
                            // }
                        ]
                    }
                    this.formData = lodash.omit(res.result, 'approvalOpinions')
                    this.$refs['contractTaskForm'].getForm().setFieldsValue(pick(res.result, 'contractCode','contractName','equipNum','isLocal','partyA','signedDate','budgetAmount','contractAnnex'))
                    // this.formAttributes.formItems[0].fields[8].items = res.result.approvalOpinions || []
                    this.approvalOpinions = res.result.approvalOpinions
                
                    //显示撤销按钮
                    if(this.formData.applicationEmpNo==this.taskInfo.assignee){
                        this.isCancel=true
                    }else{
                        this.isCancel=false
                    }

                    this.setDisabled()
                    //查询详情
                    this.$refs['list'].setInitQuery([{field:'contractCode',val:this.formData.contractCode}])
                    this.$refs['list'].searchQuery()

                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
        close() {
            this.visible = false;
            this.$emit('close');
            
        },
      
        cancel(){
            let that = this
            Modal.confirm({
                title: "系统提醒",
                content: "撤销后数据将被删除，确定撤销?",
                onOk: () => {
                   that.confirmLoading = true
                   request.get('/WxContractController/cancel',{params:{instanceId: this.taskInfo.procInstId}}).then((res)=>{
                        if (res.success) {
                            that.isShowButton = false
                            that.isCancel = false
                            that.close()
                            that.$message.success(res.message);
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false
                        that.setDisabled()
                    })
                },
                onCancel () {}
            })
        },
        setDisabled(){
            if(this.isCancel){
                //可编辑
                this.formAttributes.formItems[0].fields[0].disabled=false
                this.formAttributes.formItems[0].fields[1].disabled=false
                this.formAttributes.formItems[0].fields[2].disabled=false
                this.formAttributes.formItems[0].fields[3].disabled=false
                this.formAttributes.formItems[0].fields[4].disabled=false
                this.formAttributes.formItems[0].fields[5].disabled=false
                this.formAttributes.formItems[0].fields[6].disabled=false
                this.formAttributes.formItems[1].fields[0].disabled=false
            }else{
                this.formAttributes.formItems[0].fields[0].disabled=true
                this.formAttributes.formItems[0].fields[1].disabled=true
                this.formAttributes.formItems[0].fields[2].disabled=true
                this.formAttributes.formItems[0].fields[3].disabled=true
                this.formAttributes.formItems[0].fields[4].disabled=true
                this.formAttributes.formItems[0].fields[5].disabled=true
                this.formAttributes.formItems[0].fields[6].disabled=true
                this.formAttributes.formItems[1].fields[0].disabled=true
            }
        },
        edit(record){
            this.sysPageOpen('1744259764370747394',record,'ExtModal','车辆详情')
        }
    }
}

</script>


